package cn.yudao.system.controller;

import cn.yudao.basic.annotation.PreAuthorize;
import cn.yudao.system.service.IMenuService;
import cn.yudao.system.domain.Menu;
import cn.yudao.system.query.MenuQuery;
import cn.yudao.basic.utils.AjaxResult;
import cn.yudao.basic.utils.PageList;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/menu")
public class MenuController {
    @Autowired
    public IMenuService menuService;


    /**
     * 保存和修改公用的
     * @param menu  传递的实体
     * @return Ajaxresult转换结果
     */
    @PutMapping
    @PreAuthorize(name = "新增/修改菜单",sn = "Menu:addOrupt")
    public AjaxResult addOrupadte(@RequestBody Menu menu){
        System.out.println(menu.getId());
        try {
            if( menu.getId()!=null)
                menuService.upadte(menu);
            else
                menuService.save(menu);
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("系统繁忙");
        }
    }
    /**
    * 删除对象信息
    * @param id
    * @return
    */
    @DeleteMapping(value="/{id}")
    @PreAuthorize(name = "删除菜单",sn = "Menu:delete")
    public AjaxResult delete(@PathVariable("id") Long id){
        try {
            menuService.removeById(id);
            return AjaxResult.me();
        } catch (Exception e) {
        e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("系统繁忙");
        }
    }
	
    //获取用户
    @GetMapping("/{id}")
    public Menu get(@PathVariable("id")Long id)
    {
        return menuService.LoadById(id);
    }


    /**
    * 查看所有的员工信息
    * @return
    */
    @GetMapping()
    @PreAuthorize(name = "查询所有的菜单",sn = "Menu:list")
    public List<Menu> list(){

        return menuService.loadAll();
    }


    /**
    * 分页查询数据
    *
    * @param query 查询对象
    * @return PageList 分页对象
    */
    @PostMapping("/list")
    @PreAuthorize(name = "查询菜单",sn = "Menu:query")
    public PageList<Menu> json(@RequestBody MenuQuery query)
    {
        return menuService.query(query);
    }


    @GetMapping("/tree")
    public List<Menu> loadTree(){
        return menuService.loadTree();
    }

    @PatchMapping
    public AjaxResult patchRom(@RequestBody Long[] ids){
        try {
            menuService.patchRom(ids);
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("系统繁忙");
        }
    }
}


